<?php

namespace app\speedDating\controller\index;

use app\speedDating\AppController;
use app\speedDating\services\MemberService;
use app\speedDating\services\StoreService;
use app\speedDating\web\JsonResult;

class IndexController extends AppController
{
    /**
     * 注册并返回用户token
     * @param MemberService $service
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function register(MemberService $service)
    {
        $data['username'] = request()->param('username');
        $data['password'] = request()->param('password');
        $data['password_confirmation'] = request()->param('password_confirmation');

        $this->validate(
            $data,
            [
                'username' => 'require|mobile',
                'password' => 'require|alphaNum',
                'password_confirmation' => 'require|alphaNum|confirm:password',
            ],
            [
                'username.require' => '请填写手机号',
                'username.mobile' => '请输入正确的手机号',
                'password.require' => '请输入密码',
                'password.alphaNum' => '密码仅支持字母与数字组合',
                'password_confirmation.require' => '请输入确认密码',
                'password_confirmation.alphaNum' => '确认密码仅支持字母与数字组合',
                'password_confirmation.confirm' => '密码与确认密码不同',
            ]
        );

        return JsonResult::success($service->register($data));
    }

    /**
     * 会员登录返回token
     * @param MemberService $service
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function login(MemberService $service)
    {
        $data['username'] = request()->param('username');
        $data['password'] = request()->param('password');

        $this->validate(
            $data,
            [
                'username' => 'require|mobile',
                'password' => 'require|alphaNum',
            ],
            [
                'username.require' => '请填写手机号',
                'username.mobile' => '请输入正确的手机号',
                'password.require' => '请输入密码',
                'password.alphaNum' => '密码仅支持字母与数字组合',
            ]
        );

        return JsonResult::success($service->login($data));
    }

}